﻿class player{
	//defining characteristics of the player
	private var allianceName:String;		//the name of this player's alliance
	private var side:String;				//side the player is on
	private var wizard:unit;				//the wizard of this player (the main one)
	private var myTower:tower;				//the tower this unit has
	
	//card related stuff
	private var deck:Array;					//the deck for this player
	private var hand:Array;					//the hand for this player
	
	//timer related stuff
	private var cooldown:Number;	//the number of seconds the player must wait due to cooldown
	private var cooldownTimerID:Number;
	
	//temporary properties of the player
	private var activeSpell:spell;	//spell that will be cast if the user 'clicks'
	
	public function player(){
		cooldown = 0;
		activeSpell = null;
		deck = new Array();
		hand = new Array();
		side = "not set";
	}
	
	/**
	 * Looks at the config class and determines how many cards to draw
	 * Draws that many cards into hand	 */
	public function drawStartingHand():Void{
		for(var i = 0; i < config.handSize; i++){
			hand.push(deck.pop());
		}		
	}
	
	/**
	 * Draws the top card of the deck into hand
	 * @return the card drawn
	 * 	 */
	public function drawCard():card{
		var tmp = deck.pop();
		hand.push(tmp);	
		return tmp;
	}
	
	/**
	 * Adds the specified card to the deck
	 * @param newCard The card to be added	 */
	public function addCardToDeck(newCard:card):Void{
		deck.push(newCard);
	}
	
	/**
	 * Sets the deck for this player
	 * @param d the new value for deck	 */
	public function setDeck(d:Array):Void{
		deck = d;	
	}
	
	/**
	 * Gets the deck for this player
	 * @return the deck for this player	 */
	public function getDeck():Array{
		return deck;	
	}
	
	/**
	 * Sets the wizard for this player
	 * @param u the new value for wizard	 */
	public function setWizard(u:unit):Void{
		wizard = u;
	}
	
	/**
	 * Returns the wizard for this player
	 * @return returns the tower for this player	 */
	public function getWizard():unit{
		return wizard;	
	}
	
	/**
	 * Returns the array representing the current hand for this player
	 * @return the array representing the current hand for this player	 */
	public function getHand():Array{
		return hand;
	}
	
	/**
	 * Sets the tower for this player
	 * @param t the new value for myTower	 */
	
	public function setTower(t:tower):Void{
		myTower = t;
		t.setParent(this);	
	}
	
	/**
	 * Returns the tower for this player
	 * @return returns the tower for this player	 */
	public function getTower():tower{
		return myTower;	
	}
	
	/**
	 * Sets the 'side' this player is on (left or right)
	 * @param s The new side	 */
	public function setSide(s:String):Void{
		side = s;
	}
	
	/**
	 * Returns the side that this player is on
	 * @return The side that this player is on	 */
	public function getSide():String{
		return side;
	}
	
	/**
	 * Returns true if this player is able to cast right now
	 * ie: cooldown == 0	 */
	public function canCast():Boolean{
		if(cooldown == 0)
			return true;
		else
			return false;
	}
	
	/**
	 * Sets the active spell to the parameter
	 * @param s the new active spell	 */
	public function setActiveSpell(s:spell):Void{
		activeSpell = s;
	}
	
	/**
	 * Tells the active spell to execute at (X, Y)
	 * @param x the x-mouse coordinate
	 * @param y the y-mouse coordinate	 */
	public function activateSpell(x:Number, y:Number){
		//activates spell at location
		activeSpell.userClick(x, y);
		activeSpell = null;
	}
	
	/**
	 * Sets the alliance name of this player to n
	 * @param n the new alliance name of this player	 */
	public function setAllianceName(n:String):Void{
		allianceName = n;
	}
	
	/**
	 * Returns the alliance name for this player
	 * @return the alliance name for this player	 */
	public function getAllianceName():String{
		return allianceName;	
	}
	
	/**
	 * Returns the number of seconds left in the cooldown
	 * @return the number of seconds left in the cooldown	 */
	public function getCooldown():Number{
		return cooldown;	
	}
	/**
	 * Begins the cooldown for 'time' seconds
	 * @param the time to cooldown, in seconds	 */
	public function startCooldown(time:Number){
		cooldown = time;
		cooldownTimerID = setInterval(checkCooldown, 1000, this);
	}
	
	/**
	 * Checks the cooldown for this player; if == -1, then clear the timer
	 * @param p This must be specified because when setInterval calls this function
	 * 			it doesn't know where it came from	 */
	public function checkCooldown(p:player){
		p.cooldown -= 1;
		if(p.cooldown == 0){
			clearInterval(p.cooldownTimerID);
		}
	}
}